﻿<html>
<body>
<style type="text/css">
.classtip
{
    color:Red;
}
</style>
<table style="background-color:white;">
<tr>
<td>Username:</td><td><input id="regUsername" type="text" onblur="checkUser(this.value);" maxlength="15" /><span id="tipUsername" class="classtip"></span>
</td>
</tr>
<tr>
<td></td><td><span style="color:gray;">Username can only consist of letters and numbers</span> </td>
</tr>
<tr>
<td>Nickname:</td><td><input id="regNickname" type="text"/></td>
</tr>
<tr>
<td>E-Mail:</td><td><input id="regEmail" type="text" onblur="checkMail(this.value);"/><span id="tipMail" class="classtip"></span></td>
</tr>
<tr>
<td>Password:</td><td><input id="regPassword" type="password" onblur="checkPassw(this.value);"/><span id="tipPassword" class="classtip"></span></td>
</tr>
<tr>
<td>Re-type Password:</td><td><input id="regRepassword" type="password" onblur="checkPassw2(this.value)"/><span id="tipRepassword" class="classtip"></span></td>
</tr>
<tr>
<td><input onclick="registerUser()" value="OK" type="button" /></td><td><input value="Cancel" type="button"/></td>
</tr>
</table>
<h6 id="waitSecondID" style="display:none">redirect to the new page after  <span style="color:Red" id="totalSecond">3</span> seconds.</h6>

<script type="text/javascript">
    var a = false, b = false, c = false, d = false, e=false;
    function $id(id) { return document.getElementById(id); };
    function getCharLen(str) {
        var len = 0;
        for (var i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
                len += 2;
            } 
            else {
                len++;
            }
        }
        return len;
    }

    function checkUser(name) {
        verifyUsername(name);
        var nameRegex = /^(?!_)(?!.*?_$)[a-zA-Z0-9]+$/;
        var len = getCharLen(name);
        if (name == 0) {
            $id("tipUsername").innerHTML = "用户名不能为空!";
            a = false;
        } 
        else if (!nameRegex.test(name)) {
            $id("tipUsername").innerHTML = "用户名只能用英文、数字及下划线!";
            a = false;
        } 
        else if (len < 4 || len > 15) {
            $id("tipUsername").innerHTML = "用户名长度为4-15个字符!";
            a = false;
        } 
        else {
            $id("tipUsername").innerHTML = "";
            a = true;
        }
    }

    function checkPassw(pass) {
        var allNumRegex = /^\d+$/.test(pass);
        var haveNumRegex = /\d/.test(pass);
        var allhaveAbcRegex = /^[a-zA-Z]+$/.test(pass);
        var haveAbcRegex = /[a-zA-Z]/.test(pass);
        var haveStrongRegex = /[^a-zA-Z0-9]/.test(pass);
        var veryStrongRegex = pass.split(/[^a-zA-Z_0-9]/).length > 2;
        var len = getCharLen(pass);
        if (len == 0) {
            $id("tipPassword").innerHTML = "密码不能为空!";
            b = false;
        } 
        else if (len < 6 || len > 20) {
            $id("tipPassword").innerHTML = "用户名长度为6-20个字符!";
            b = false;
        } 
        else if ((allNumRegex || allhaveAbcRegex) || (haveNumRegex && haveAbcRegex && len < 8)) {
            $id("tipPassword").innerHTML = "密码强度弱";
            b = true;
        } 
        else {
            $id("tipPassword").innerHTML = "";
            b = true;
        }
        if ((haveNumRegex && haveAbcRegex && len >= 8) || (haveNumRegex && haveAbcRegex && haveStrongRegex && len < 8) || ((haveNumRegex && haveStrongRegex) || (haveAbcRegex && haveStrongRegex))) {
            $id("tipPassword").innerHTML = "<em style='color:#ff9900;'>密码强度中</em>";
        }
        if ((haveNumRegex && haveAbcRegex && haveStrongRegex && len >= 8) || (haveNumRegex && haveAbcRegex && veryStrongRegex && len < 8) || (((haveNumRegex && haveStrongRegex) || (haveAbcRegex && haveStrongRegex)) && len >= 8)) {
            $id("tipPassword").innerHTML = "<em style='color:#00cc00;'>密码强度强";
        }
    }

    function checkPassw2(rep) {
        var passWord = $id("regPassword").value;
        if (rep == 0) {
            $id("tipRepassword").innerHTML = "确认密码不能为空!";
            c = false;
        } 
        else if (rep != passWord) {
            $id("tipRepassword").innerHTML = "确认密码不一致";
            c = false;
        } 
        else {
            $id("tipRepassword").innerHTML = "";
            c = true;
        }
    }

    function checkMail(mail) {
        var mailRegex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        if (mail == 0) {
            $id("tipMail").innerHTML = "邮箱不能为空!";
            d = false;
        } else if (!mailRegex.test(mail)) {
            $id("tipMail").innerHTML = "邮箱格式不正确!";
            d = false;
        } else {
            $id("tipMail").innerHTML = "";
            d = true;
        }
    }

    function responseRegister(resTxt) {
        switch (resTxt) {
            case "2":
                var tmp = $id("waitSecondID");
                tmp.style.display = "";
                if (parent.Sys.ie) {
                    second = $id("totalSecond").innerText;
                }
                else {
                    second = $id("totalSecond").textContent;
                }
                setInterval(function () {
                    redirect(url, second);
                    second--;
                }, 1000);
                break;
        }
    }

    function registerUser() {
        if (a && b && c && d) {
            var second;
            var url;
            var returnValue = "2";
            parent.sendAndGetData(parent.g.domainDirectory + "/register.aspx?currentUser=0&&username=" + $id("regUsername").value + "&&nickname=" + $id("regNickname").value + "&&email=" + $id("regEmail").value + "&&password=" + $id("regPassword").value, responseRegister);
            switch (returnValue) {
                case "2":
                    parent.$("waitSecondID").style.display = "";
                    if (Sys.ie) {
                        second = parent.$("totalSecond").innerText;
                    }
                    else {
                        second = parent.$("totalSecond").textContent;
                    }
                    setInterval(function () {
                        redirect(url, second);
                        second--;
                    }, 1000);
                    break;
            }
        }
    }

    function verifyUsername(username) {
        parent.sendAndGetData(parent.g.domainDirectory + "/register.aspx?act=0&&username=" + $id("regUsername").value, resVerifyUsername);
    }

    function resVerifyUsername(resTxt) {
        switch (resTxt) {
            case "2":
                d = false;
                break;
            default:
                break;
        }
    }

</script>
</body>
</html>